package com.huige.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huige.mapper.ScheduleMapper;
import com.huige.pojo.Schedule;
import com.huige.service.ScheduleService;
import com.huige.utils.PageBean;
import com.huige.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.List;

/**
 * @Author 朱志辉
 * @Create 2024/7/21 下午2:10
 * @Version 1.0
 * Description:
 */
@Service
@Slf4j
public class ScheduleServiceImpl implements ScheduleService {


    @Autowired
    private ScheduleMapper scheduleMapper;

    /**
     * 查询全部数据页数据
     *
     * @param pageSize
     * @param currentPage
     * @return
     */
    @Override
    public R page(int pageSize, int currentPage) {
        //分页
        PageHelper.startPage(currentPage, pageSize);
        //查询
        List<Schedule> schedules = scheduleMapper.queryPage();
        //分页数据封装
        PageInfo<Schedule> info = new PageInfo<>(schedules);

        //装配PageBean
        PageBean<Schedule> pageBean = new PageBean<>(currentPage, pageSize, info.getTotal(), info.getList());
        R r = R.ok(pageBean);
        return r;
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    @Transient
    public R deleteById(int id) {
       int row= scheduleMapper.deleteById(id);
       if (row>0){
           return R.ok("删除成功");
       }
        return R.fail("删除失败");
    }

    /**
     * 添加数据
     *
     * @param schedule
     * @return
     */
    @Override
    public R addSchedule(Schedule schedule) {
        int row = scheduleMapper.addSchedule(schedule);
        if (row > 0) {
            return R.ok("添加成功");
        }
        return R.fail("添加失败");
    }

    /**
     * 修改数据
     *
     * @param schedule
     * @return
     */
    @Override
    public R update(Schedule schedule) {
        //判断id不能为null
        if (schedule.getId() == null) {
            return R.fail("id不能为空");
        }
        int row = scheduleMapper.update(schedule);
        if (row > 0) {
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }
}
